package com.fishingwithme.controllers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.fishingwithme.domain.common.UserComment;
import com.fishingwithme.domain.common.UserCommentService;
import com.fishingwithme.infrastructure.dtos.DTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/comments")
@Tag(name = "Comments", description = "APIs for managing user comments and interactions")
public class CommentController {

    @Autowired
    private UserCommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping(path = "/add")
    @Operation(summary = "Add new comment", description = "Create a new comment on a target resource", security = @SecurityRequirement(name = "Bearer Authentication"))
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Comment created successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = DTO.class))),
            @ApiResponse(responseCode = "401", description = "Unauthorized - JWT token required")
    })
    public @ResponseBody DTO<UserComment> addComment(
            @Parameter(description = "Comment information", required = true) @RequestBody UserComment comment) {
        UserComment savedComment = commentService.addComment(comment);
        return DTO.success(savedComment);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete")
    public @ResponseBody DTO<Boolean> deleteComment(@RequestParam Long commentId, @RequestParam Long userId) {
        boolean success = commentService.deleteComment(commentId, userId);
        return success ? DTO.success(true) : DTO.fail("删除失败");
    }

    /**
     * 更新评论内容
     */
    @PutMapping("/update")
    public @ResponseBody DTO<Boolean> updateComment(@RequestParam Long commentId,
            @RequestParam Long userId,
            @RequestBody Map<String, String> contentMap) {
        String content = contentMap.get("content");
        boolean success = commentService.updateCommentContent(commentId, userId, content);
        return success ? DTO.success(true) : DTO.fail("更新失败");
    }

    /**
     * 获取带层级的评论列表（盖楼效果）
     */
    @GetMapping(path = "/list")
    public @ResponseBody DTO<IPage<UserComment>> getCommentsWithHierarchy(
            @RequestParam String targetType,
            @RequestParam Long targetId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum < 1)
            pageNum = 1;
        if (pageSize < 1 || pageSize > 100)
            pageSize = 20;
        IPage<UserComment> commentPage = commentService.getCommentsWithHierarchy(targetType, targetId, pageNum,
                pageSize);
        return DTO.success(commentPage);
    }

    /**
     * 分页获取子评论
     */
    @GetMapping("/children")
    public @ResponseBody DTO<IPage<UserComment>> getChildComments(
            @RequestParam Long parentId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum < 1)
            pageNum = 1;
        if (pageSize < 1 || pageSize > 100)
            pageSize = 20;
        IPage<UserComment> childPage = commentService.getChildCommentsByPage(parentId, pageNum, pageSize);
        return DTO.success(childPage);
    }

    /**
     * 获取评论详情
     */
    @GetMapping("/detail")
    public @ResponseBody DTO<UserComment> getCommentDetail(@RequestParam Long commentId) {
        UserComment comment = commentService.getCommentById(commentId);
        return comment != null ? DTO.success(comment) : DTO.fail("评论不存在");
    }

    /**
     * 获取用户发布的评论
     */
    @GetMapping("/user/get")
    public @ResponseBody DTO<IPage<UserComment>> getUserComments(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum < 1)
            pageNum = 1;
        if (pageSize < 1 || pageSize > 100)
            pageSize = 20;
        IPage<UserComment> commentPage = commentService.getUserComments(userId, pageNum, pageSize);
        return DTO.success(commentPage);
    }

    /**
     * 评论点赞
     */
    @GetMapping("/like")
    public @ResponseBody DTO<Boolean> likeComment(@RequestParam Long commentId) {
        return DTO.success(commentService.likeComment(commentId));
    }

    /**
     * 取消评论点赞
     */
    @GetMapping("/unlike")
    public @ResponseBody DTO<Boolean> unlikeComment(@RequestParam Long commentId) {
        return DTO.success(commentService.unlikeComment(commentId));
    }
}